var log4js = require('./utils/logs');
var createError = require('http-errors');
var express = require('express');
var swaggerUi = require('swagger-ui-express');
var swaggerJSDoc = require('swagger-jsdoc');
var path = require('path');
var cookieParser = require('cookie-parser');
// var logger = require('morgan');
var compression = require('compression');
var filter = require('./utils/filter');

var schemasRouter = require('./routes/schemas');
var instancesRouter = require('./routes/instances');
var categoriesRouter = require('./routes/categories');
var datatypesRouter = require('./routes/datatypes');
var relationsRouter = require('./routes/relations');
var relationtypesRouter = require('./routes/relationtypes');
var relationinstancesRouter = require('./routes/relationinstances');
var tagsRouter = require('./routes/tags');
var businessesRouter = require('./routes/businesses');
var topolayoutsRouter = require('./routes/topolayouts');
var bizdefsRouter = require('./routes/bizdefs');
var bizsRouter = require('./routes/bizs');
var bizrelasRouter = require('./routes/bizrelas');
var bizrelainstsRouter = require('./routes/bizrelainsts');

var healthRouter = require('./routes/health');

var app = express();

var swaggerSpec = swaggerJSDoc({
  swaggerDefinition: {
  	basePath: '/'
  },
  apis: [path.join(__dirname, '/routes/*.js')]
})
app.use('/swagger-ui.html', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// view engine setup
app.use(compression());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// app.use(logger('dev'));
// app.use(logger(':date[clf] :method :url HTTP/:http-version :status :res[content-length] - :response-time ms'));
app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto' }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(filter.authUser);

app.use('/cmdb/schemas', schemasRouter);
app.use('/schemas', schemasRouter);
app.use('/cmdb/instances', instancesRouter);
app.use('/instances', instancesRouter);
app.use('/cmdb/categories', categoriesRouter);
app.use('/categories', categoriesRouter);
app.use('/cmdb/datatypes', datatypesRouter);
app.use('/datatypes', datatypesRouter);
app.use('/cmdb/relationtypes', relationtypesRouter);
app.use('/relationtypes', relationtypesRouter);
app.use('/cmdb/relations', relationsRouter);
app.use('/relations', relationsRouter);
app.use('/cmdb/relationinstances', relationinstancesRouter);
app.use('/relationinstances', relationinstancesRouter);
app.use('/cmdb/tags', tagsRouter);
app.use('/tags', tagsRouter);
app.use('/cmdb/businesses', businessesRouter);
app.use('/businesses', businessesRouter);
app.use('/cmdb/topolayouts', topolayoutsRouter);
app.use('/topolayouts', topolayoutsRouter);
app.use('/cmdb/health', healthRouter);
app.use('/health', healthRouter);

app.use('/cmdb/bizdefs', bizdefsRouter);
app.use('/bizdefs', bizdefsRouter);
app.use('/cmdb/bizs', bizsRouter);
app.use('/bizs', bizsRouter);
app.use('/cmdb/bizrelas', bizrelasRouter);
app.use('/bizrelas', bizrelasRouter);
app.use('/cmdb/bizrelainsts', bizrelainstsRouter);
app.use('/bizrelainsts', bizrelainstsRouter);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  //res.render('error');
  res.json({ status: 'failure', data: err.message })
});

module.exports = app;
